{% extends 'layout/basic.html' %}
{% load static %}

{% block title %} 用户邮箱登录 {% endblock %}

{% block css %}
    <link rel="stylesheet" href="{% static 'css/account.css' %}">
    <style>
        .error-msg {
            color: red;
            position: absolute;
            font-size: 13px;
        }
    </style>
{% endblock %}

{% block content %}
    <div class="account">
        <div class="title">用户邮箱登录</div>
        <form id="emailForm" method="POST" novalidate>
            {% csrf_token %}
            {% for field in form %}
                {% if field.name == 'code' %}
                    <div class="form-group">

                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        <div class="row">
                            <div class="col-xs-7">
                                {{ field }}
                                <span class="error-msg"></span>
                            </div>
                            <div class="col-xs-5">
                                <input id='btnEmail' type="button" class="btn btn-default" value="点击获取验证码">
                            </div>
                        </div>
                    </div>
                {% else %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        {{ field }}
                        <span class="error-msg"></span>
                    </div>
                {% endif %}
            {% endfor %}
            <div>
                <div style="float:right;">
                    <a href="{% url 'login' %}">用户名密码登录?</a>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-3">
                    <input id="btnSubmit" type="button" class="btn btn-primary" value="登 录">
                </div>
            </div>
        </form>
    </div>
{% endblock %}

{% block js %}
    <script>
        $(function () {
            bindClickBtnEmail();
            bindClickSubmit();
        })

        function bindClickSubmit() {
            $('#btnSubmit').click(function () {
                $('.error-msg').empty()
                $.ajax({
                    url: '{% url 'login_email' %}',
                    type: 'POST',
                    data: $('#emailForm').serialize(),
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.status) {
                            location.href = res.data;
                        } else {
                            $.each(res.error, function (key, value) {
                                $('#id_' + key).next().text(value[0]);
                            });
                        }
                    }
                })
            })
        }

        function bindClickBtnEmail() {
            $('#btnEmail').click(function () {
                $('.error-msg').empty()
                var email = $('#id_email').val();
                $.ajax({
                    url: '{% url 'send_email' %}',
                    type: 'GET',
                    data: {email: email, tpl: 'login'},
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.status) {
                            //倒计时效果
                            sendEmailRemind();
                        } else {
                            $.each(res.error, (function (key, value) {
                                $('#id_' + key).next().text(value[0]);
                            }));
                        }
                    }

                })
            });
        }

        function sendEmailRemind() {
            var $emailBtn = $('#btnEmail')
            $emailBtn.prop('disabled', true) //禁用
            var time = 60;
            var remaind = setInterval(function () {
                $emailBtn.val(time + '秒重新发送');
                time = time - 1;
                if (time < 1) {
                    clearInterval(remaind);
                    $emailBtn.val('点击获取验证码').prop('disabled', false)
                }
            }, 1000);
        }
    </script>
{% endblock %}